import {vnode} from "./vnode";
import {createElement} from "./createElement";
import {patchVnode} from "./patchVnode";
import {sameVnode} from "./sameNode";


export function patch(oldNode, newNode) {
    //判断旧节点是否为dom节点
    if (oldNode instanceof HTMLElement) {
        oldNode = vnode(oldNode.tagName.toLocaleLowerCase(), {}, [], undefined, oldNode);
    }
    //判断新老节点是否为同一个节点
    if (sameVnode(oldNode, newNode)) {
        patchVnode(oldNode, newNode);

    } else {
        let newNodeDom = createElement(newNode);
        if (newNodeDom) {
            oldNode.elm.parentNode.insertBefore(newNodeDom, oldNode.elm);
        }
        //删除老节点
        oldNode.elm.parentNode.removeChild(oldNode.elm);
    }
}
